package routers

import (
	"team-evaluate/global"
	"team-evaluate/internal/routers/api"
	v1 "team-evaluate/internal/routers/api/v1"
	"team-evaluate/pkg/middleware"

	ginSwagger "github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"

	"github.com/gin-gonic/gin"

	_ "team-evaluate/docs"
)

func NewRouter() *gin.Engine {
	r := gin.New()

	// 允许跨域
	r.Use(middleware.Cors())
	r.Use(middleware.AppInfo())
	if global.ServerSetting.RunMode == "debug" {
		r.Use(gin.Logger())
		r.Use(gin.Recovery())
	} else {
		r.Use(middleware.Recovery())
	}
	r.Use(middleware.AccessLog())

	// 配置静态文件目录
	r.Static("/public", "public")

	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	// 登录
	r.POST("/login", api.Login)
	user := v1.NewUser()
	r.POST("/api/v1/user", user.Create)
	team := v1.NewTeam()
	userTeam := v1.NewUserTeam()

	apiv1 := r.Group("/api/v1")
	apiv1.Use(middleware.JWT())
	{
		apiv1.GET("/users", user.List)
		apiv1.GET("/user/:id", user.FindById)
		apiv1.PUT("/user/:id", user.Update)
		apiv1.DELETE("/user/:id", user.Delete)

		apiv1.GET("/team/:id", team.FindById)
		apiv1.GET("team/name", team.FindByName)
		apiv1.GET("/teams", team.List)
		apiv1.POST("/team", team.Create)
		apiv1.PUT("/team/:id", team.Update)
		apiv1.DELETE("/team/:id", team.Delete)

		apiv1.POST("/user-team", userTeam.Create)
		apiv1.GET("/user-teams/users", userTeam.UserList)
		apiv1.GET("/user-teams/teams", userTeam.TeamList)
		apiv1.GET("/user-teams/teams-byname", userTeam.TeamListByTeamName)
		apiv1.PUT("/user-team/agree/:id", userTeam.Agree)     // 创建团队者同意用户加入团队
		apiv1.DELETE("user-team/:id", userTeam.Delete)        // 创建团队者拒绝用户加入团队
		apiv1.DELETE("user-team/leavel", userTeam.LeavelTeam) // 用户从团队离开
	}

	return r
}
